<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
      class="white-normal-box">
      <!-- <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="用户账号" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      <!-- </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入用户邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--   <el-form-item label="头像地址" prop="avatar">
        <el-input
          v-model="queryParams.avatar"
          placeholder="请输入头像地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后登录IP" prop="loginIp">
        <el-input
          v-model="queryParams.loginIp"
          placeholder="请输入最后登录IP"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后登录时间" prop="loginDate">
        <el-date-picker clearable
          v-model="queryParams.loginDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后登录时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="white-normal-box">
      <el-row :gutter="10" class="mb20">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['system:users:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['system:users:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['system:users:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['system:users:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户ID" align="center" prop="userId" v-if="false" />
        <!--  <el-table-column label="部门ID" align="center" prop="deptId" /> -->
        <el-table-column label="用户账号" align="center" prop="userName" />
        <!-- <el-table-column label="用户昵称" align="center" prop="nickName" /> -->
        <!--  <el-table-column label="用户类型" align="center" prop="userType" /> -->
        <el-table-column label="用户邮箱" align="center" prop="email" />
        <el-table-column label="手机号码" align="center" prop="phonenumber" />
        <el-table-column label="用户性别" align="center" prop="sex" />
        <!-- <el-table-column label="头像地址" align="center" prop="avatar" /> -->
        <!-- <el-table-column label="密码" align="center" prop="password" /> -->
        <el-table-column label="帐号状态" align="center" prop="status">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 0">正常</span>
            <span v-if="scope.row.status == 1">停用</span>

          </template>
        </el-table-column>
        <el-table-column label="最后登录IP" align="center" prop="loginIp" />
        <el-table-column label="最后登录时间" align="center" prop="loginDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="金币等级" align="center" prop="remark">
          <template slot-scope="scope">
            <span v-if="scope.row.remark == 0">未设置</span>
            <span v-if="scope.row.remark == 1">默认</span>
            <span v-if="scope.row.remark == 2">等级一</span>
            <span v-if="scope.row.remark == 3">等级二</span>
            <span v-if="scope.row.remark == 4">等级三</span>
          </template>
        </el-table-column>
        <el-table-column label="机器数量" align="center" prop="jiqi" />
        <el-table-column label="支付金额" align="center" prop="jine" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:users:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:users:remove']">删除</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleShiming(scope.row)"
              v-hasPermi="['system:users:shiMing']">查看实名</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="用户账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="头像地址" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像地址" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="最后登录IP" prop="loginIp">
          <el-input v-model="form.loginIp" placeholder="请输入最后登录IP" />
        </el-form-item>
        <el-form-item label="最后登录时间" prop="loginDate">
          <el-date-picker clearable
            v-model="form.loginDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后登录时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="金币等级" prop="remark">
          <el-radio-group v-model="form.remark">
            <!--  <el-radio label="0">未设置</el-radio> -->
            <el-radio label="1">默认</el-radio>
            <el-radio label="2">等级一</el-radio>
            <el-radio label="3">等级二</el-radio>
            <el-radio label="4">等级三</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="openss" width="900px" append-to-body>
      <el-table v-loading="loading" :data="forms" :row-key="getRowKeys" :expand-row-keys="expands">
        <!-- <el-form ref="forms" :model="forms" :rules="rules" label-width="80px">
        <el-table-column label="营业执照图片" align="center" prop="businessLicense" /> -->
        <el-table-column label="企业名称" align="center" prop="enterpriseName" min-width="160" show-overflow-tooltip />
        <el-table-column label="统一社会信用代码" align="center" prop="credit" min-width="160" show-overflow-tooltip />
        <el-table-column label="营业执照图片" prop="businessLicense" align="center" width="100px">
          <template slot-scope="scope">
            <image-preview :src="scope.row.businessLicense" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="userName" min-width="120" />
        <el-table-column label="身份证号" align="center" prop="identityCard" min-width="120" />
        <el-table-column label="手机号码" align="center" prop="phoneNumber" min-width="120" />
        <!-- <el-table-column label="正面照片" align="center" prop="fullFacePhoto" /> -->
        <el-table-column label="正面照片" prop="fullFacePhoto" align="center" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.fullFacePhoto" :width="50" :height="50" />
          </template>
        </el-table-column>
        <!--  <el-table-column label="反面照片" align="center" prop="backPhoto" /> -->
        <el-table-column label="反面照片" prop="backPhoto" align="center" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.backPhoto" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="地址" align="center" prop="site" min-width="100" show-overflow-tooltip />
        <!-- <el-table-column label="预留字段" align="center" prop="remok" /> -->
        <el-table-column label="实名类型" align="center" prop="remok">
          <template slot-scope="scope">
            <span v-if="scope.row.remok == 0">个人</span>
            <span v-if="scope.row.remok == 1">对公</span>
            <span v-if="scope.row.remok == 2">法人</span>
          </template>
        </el-table-column>
        <!--
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="forms.enterpriseName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="credit">
          <el-input v-model="forms.credit" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="营业执照图片" prop="businessLicense">
          <el-input v-model="forms.businessLicense" placeholder="请输入营业执照图片" />
        </el-form-item>
        <el-form-item label="法人姓名" prop="userName">
          <el-input v-model="forms.userName" placeholder="请输入法人姓名" />
        </el-form-item>
        <el-form-item label="法人身份证" prop="identityCard">
          <el-input v-model="forms.identityCard" placeholder="请输入法人身份证" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phoneNumber">
          <el-input v-model="forms.phoneNumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="正面照片" prop="fullFacePhoto">
          <el-input v-model="forms.fullFacePhoto" placeholder="请输入正面照片" />
        </el-form-item>
        <el-form-item label="反面照片" prop="backPhoto">
          <el-input v-model="forms.backPhoto" placeholder="请输入反面照片" />
        </el-form-item>
        <el-form-item label="地址" prop="site">
          <el-input v-model="forms.site" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="forms.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="预留字段" prop="remok">
          <el-input v-model="forms.remok" placeholder="请输入预留字段" />
        </el-form-item> -->
      </el-table>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> --> 
        <el-button @click="cancels">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, shiMing } from "@/api/system/users";

export default {
  name: "User",
  data() {
    return {
      getRowKeys(row) {
        return row.num;
      },
      expands: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openss: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        userName: null,
        nickName: null,
        userType: null,
        email: null,
        phonenumber: null,
        sex: null,
        avatar: null,
        password: null,
        status: null,
        loginIp: null,
        loginDate: null,
        jiqi: null,
        jine: null,

      },
      // 表单参数
      form: {},
      forms: {},
      // 表单校验
      rules: {
      },
      srcList: ["https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg"]

    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 添加默认展开行
    expandRows() {
      // 换页时置空数组
      this.expands = []
      // 给每行添加key值(num)
      for (let i = 0; i < this.forms.length; i++) {
        this.forms[i].num = i;
      }
      // 添加默认展开行
      this.forms.forEach((item) => {
        if (item.id.是否有子设备) {
          this.expands.push(item.num);
        }
      });
    },


    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancels() {
      this.openss = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        deptId: null,
        userName: null,
        nickName: null,
        userType: null,
        email: null,
        phonenumber: null,
        sex: null,
        avatar: null,
        password: null,
        status: null,
        delFlag: null,
        loginIp: null,
        loginDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    resets() {
      this.form = {
        enterpriseName: null,
        credit: null,
        businessLicense: null,
        userName: null,
        identityCard: null,
        phoneNumber: null,
        fullFacePhoto: null,
        backPhoto: null,
        site: null,
        userId: null,
        remok: null
      };
      this.resetsForm("forms");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getUser(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户信息";
      });
    },
    /* 查看实名*/
    handleShiming(row) {
      this.reset();
      const userId = row.userId || this.ids
      shiMing(userId).then(response => {
        this.forms = response.data;
        this.expandRows();
        this.openss = true;
        this.title = "查看实名";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除用户信息编号为"' + userIds + '"的数据项？').then(function () {
        return delUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    bigImg(url) {
      this.srcList[0] = url;
      Error("测试" + url)
    }
  }
};
</script>
